package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.financing.wap.dto.*;
import com.financing.wap.service.IDstMemberSendLogService;
import com.financing.wap.service.IDstUserIntegralFlowService;
import com.financing.wap.service.IDstUserWithdrawLogService;
import com.financing.wap.util.DateUtil;
import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import sun.util.logging.resources.logging;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dao.IDstMemberInfoDao;
import com.financing.wap.service.IDstMemberInfoService;
import com.financing.wap.util.DTOdateSetUtil;
/**
 * dst_member_info  
 * 会员信息表 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Service("dstMemberInfoService")
public class DstMemberInfoServiceImpl implements IDstMemberInfoService{

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IDstMemberInfoDao dstMemberInfoDao;

	@Autowired
	private IDstUserWithdrawLogService dstUserWithdrawLogService;

	@Autowired
	private IDstUserIntegralFlowService dstUserIntegralFlowService;

	@Autowired
	private IDstMemberSendLogService dstMemberSendLogService;

	@Override
	public int insert(DstMemberInfoDTO dstMemberInfo) throws SQLException {
		DTOdateSetUtil.setCreateInfo(dstMemberInfo);
		DTOdateSetUtil.setUpdateInfo(dstMemberInfo);
		dstMemberInfo.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		return dstMemberInfoDao.insert(dstMemberInfo);
	}

	@Override
	public void update(DstMemberInfoDTO dstMemberInfo) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstMemberInfo);
		dstMemberInfoDao.update(dstMemberInfo);
	}

	@Override
	public void updateForce(DstMemberInfoDTO dstMemberInfo) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstMemberInfo);
		dstMemberInfoDao.updateForce(dstMemberInfo);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		dstMemberInfoDao.delete(id);
	}
	
	@Override
	public List<DstMemberInfoDTO> queryList(DstMemberInfoDTO dstMemberInfo) throws SQLException {
		return dstMemberInfoDao.queryList(dstMemberInfo);
	}

	@Override
	public int queryListCount(DstMemberInfoDTO dstMemberInfo) throws SQLException {
		return dstMemberInfoDao.queryListCount(dstMemberInfo);
	}

	@Override
	public DstMemberInfoDTO loadById(Integer id) throws SQLException {
		return dstMemberInfoDao.loadById(id);
	}
 	@Override
	public DstMemberInfoDTO queryListGetFirst(DstMemberInfoDTO dstMemberInfo)
			throws SQLException {
		 List<DstMemberInfoDTO> queryList = queryList(dstMemberInfo);
		 if(queryList==null||queryList.size()==0){
			 return new DstMemberInfoDTO();
		 }
		 
		 
		 return queryList.get(0);
	}
 	/***
 	 * 会员表 初始化
 	 */
	@Override
	public void createMember(String userOpenId) throws SQLException {
		try {
			DstMemberInfoDTO memberInfo = new DstMemberInfoDTO();
			memberInfo.setUserOpenid(userOpenId);
			memberInfo.setMemberLevel(0d);
			memberInfo.setMemberScore(0);
			memberInfo.setMemberCharge("2");
			memberInfo.setMemberDuration(0);
			memberInfo.setMemberStartDate(null);
			memberInfo.setMemberEndDate(null);
			insert(memberInfo);
		} catch (Exception e) {
			//重复唯一
		}
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void exchange(DstMemberInfoDTO dstMemberInfo, DstVirtualGoodsDTO dstVirtualGoods,Map<String,String> map)  throws  Exception{

		logger.info("user exchange begin");
		logger.info("user exchange userOpenId:"+dstMemberInfo.getUserOpenid());

		String  phone = map.get("phone");
		String  remark = map.get("remark");
		logger.info("user exchange phone:"+phone);
		logger.info("user exchange remark:"+remark);

		Date now = new Date();
		//1、积分记录表
		DstUserIntegralFlowDTO dstUserIntegralFlow =  new DstUserIntegralFlowDTO();
		dstUserIntegralFlow.setCreateDate(now);
		dstUserIntegralFlow.setUpdateDate(now);
		dstUserIntegralFlow.setCreateBy("admin");
		dstUserIntegralFlow.setUpdateBy("admin");
		dstUserIntegralFlow.setUserOpenid(dstMemberInfo.getUserOpenid());
		dstUserIntegralFlow.setType(dstVirtualGoods.getGoodsType());
		dstUserIntegralFlow.setIntegralStatus("2");
		dstUserIntegralFlow.setGrade(dstVirtualGoods.getGoodsAmount().intValue());
		dstUserIntegralFlow.setBudget("2");
		dstUserIntegralFlow.setIntegralDate(DateUtil.getDate("yyyy-MM-dd"));
		dstUserIntegralFlowService.insert(dstUserIntegralFlow);

		logger.info("user exchange dstUserIntegralFlowService insert");

		//2、商品兑换表
		DstUserWithdrawLogDTO dstUserWithdrawLog = new DstUserWithdrawLogDTO();
		dstUserWithdrawLog.setUserOpenid(dstMemberInfo.getUserOpenid());
		dstUserWithdrawLog.setUserWithdrawMoney(dstVirtualGoods.getGoodsAmount());
		dstUserWithdrawLog.setWithdrawStatus(CommonStatus.WITHDRAW_STATUS_1);
		dstUserWithdrawLog.setWithdrawType(CommonStatus.WITHDRAW_TYPE_2);
		dstUserWithdrawLog.setUserAccount(phone);
		dstUserWithdrawLog.setUserRemark(remark);
		dstUserWithdrawLog.setGoodsType(dstVirtualGoods.getGoodsType());
		dstUserWithdrawLog.setCreateDate(now);
		dstUserWithdrawLog.setUpdateDate(now);
		dstUserWithdrawLog.setCreateBy("admin");
		dstUserWithdrawLog.setUpdateBy("admin");

		logger.info("user exchange dstUserWithdrawLogService insert");

		if("sevenDay".equals(dstVirtualGoods.getGoodsType()) || "month".equals(dstVirtualGoods.getGoodsType())){

			logger.info(" executeSubscribe dstMemberSendLogService insert");

			dstMemberInfo.setMemberStartDate(dstMemberInfo.getMemberStartDate()== null ?now:dstMemberInfo.getMemberStartDate());
			Integer duration = dstMemberInfo.getMemberDuration() == null ?0:dstMemberInfo.getMemberDuration();
			dstMemberInfo.setMemberDuration(duration+Integer.valueOf(dstVirtualGoods.getGoodsField()));

			Date memberEndDate = dstMemberInfo.getMemberEndDate() == null?now :dstMemberInfo.getMemberEndDate();//延长会员时间
			memberEndDate = memberEndDate.before(now)?now:memberEndDate;
			dstMemberInfo.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));
			dstMemberInfo.setMemberLevel(Double.valueOf(dstMemberInfo.getMemberDuration()/90));//会员等级

			// 如果是 7 天，30 天会员， 状态成功  完成时间
			dstUserWithdrawLog.setWithdrawStatus(CommonStatus.WITHDRAW_STATUS_2);
			dstUserWithdrawLog.setWithdrawDate(now);
			
			// 如果是 7 天，30 天会员，直接兑换
			DstMemberSendLogDTO dstMemberSendLog = new DstMemberSendLogDTO();
			dstMemberSendLog.setUserOpenid(dstMemberInfo.getUserOpenid());
			dstMemberSendLog.setMemberBuyDate(Integer.valueOf(dstVirtualGoods.getGoodsField()));
			dstMemberSendLog.setMemberSendType(dstVirtualGoods.getGoodsType());
			dstMemberSendLog.setMemberStartDate(memberEndDate);
			dstMemberSendLog.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));
			dstMemberSendLogService.insert(dstMemberSendLog);
			
		}

		//2、商品兑换表 -add sql
		dstUserWithdrawLogService.insert(dstUserWithdrawLog);

		//3、更新会员信息
		dstMemberInfo.setMemberUseScore(dstMemberInfo.getMemberUseScore() == null?0:dstMemberInfo.getMemberUseScore()  + dstUserIntegralFlow.getGrade());
		dstMemberInfoDao.update(dstMemberInfo);

		logger.info("user exchange dstMemberInfoDao update");

		logger.info("user exchange end");

	}
}
